package com.maserb.monitor.web.jw_our;


import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwEducationInfoEntity;
import com.maserb.monitor.entity.jwOur.JwMessageEntity;
import com.maserb.monitor.enums.EducationInfoTypeEnum;
import com.maserb.monitor.enums.JwEducationInfoIsReleasEnum;
import com.maserb.monitor.enums.JwEducationStautsEnum;
import com.maserb.monitor.enums.JwMessageIsReleasEnum;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwOur.JwMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

//留言板（云端话廉）
@Controller
@RequestMapping("/jwMessage")
public class JwMessageController {
    @Autowired
    private JwMessageService service;

    @Autowired
    private IToolService toolService;



    @RequestMapping(value = {"/", "/indexMessage"})
    public ModelAndView getIndex() {
        ModelAndView view = new ModelAndView();
        view.setViewName("jw_our/message");
        JwMessageIsReleasEnum[] isReleasEnums = JwMessageIsReleasEnum.values();
        view.addObject("isReleasEnums",isReleasEnums);
        return view;
    }


    @RequestMapping("/getViewMessage/{id}")
    public ModelAndView getViewMessage(@PathVariable final String id) {
        ModelAndView view = new ModelAndView();
        view.setViewName("jw_our/message_view");
        JwMessageEntity messageEntity = service.findOne(id);
        view.addObject("entity",messageEntity);
        return view;
    }

    @RequestMapping("/doListMessage")
    @ResponseBody
    public PageResponseResult<JwMessageEntity> doListUser(PageRequestParam page, final String isRelease ,final String description) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<JwMessageEntity>>() {
            @Override
            public Page<JwMessageEntity> invoke(PageRequest var1) throws Exception {
                Page<JwMessageEntity> page = service.findAll(var1, isRelease,description);
                for(JwMessageEntity entity:page.getContent()){
                    entity.setCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if(null!=entity.getIsRelease() && !"".equals(entity.getIsRelease())) {
                        entity.setIsRelease(JwMessageIsReleasEnum.getNameByValue(Integer.parseInt(entity.getIsRelease())));
                    }
                }
                return page;
            }
        });
    }
    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult<Boolean> doDelete(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                service.deleteAllByKeys(keys);
                return true;
            }
        });
    }

    @RequestMapping("/doRelease")
    @ResponseBody
    public AjaxResult<Boolean> doRelease(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                keys.forEach(id -> service.doRelease(id));
                return true;
            }
        });
    }

    @RequestMapping("/doNoRelease")
    @ResponseBody
    public AjaxResult<Boolean> doNoRelease(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                keys.forEach(id -> service.doNoRelease(id));
                return true;
            }
        });
    }

}
